package com.yicong.ants.utils;

import android.content.DialogInterface;
import android.graphics.Bitmap;
import android.view.View;

import com.blankj.utilcode.util.ToastUtils;
import com.cchao.simplelib.core.UiHelper;
import com.cchao.simplelib.ui.activity.BaseActivity;
import com.tbruyelle.rxpermissions2.RxPermissions;
import com.yicong.ants.Constants;

public class FileHelper {
    public static void saveViewToDirPic(BaseActivity activity, View view) {
        activity.addSubscribe(new RxPermissions(activity)
                .request(Constants.READ_WRITE)
                .subscribe(aBoolean -> {
                    if (!aBoolean) {
                        activity.showToast("请授予应用存储读写权限");
                        return;
                    }
                    BitmapUtils.saveBitmap(activity, BitmapUtils.loadView2Bitmap(view));
                }));
    }

    public static void saveViewToDirPic(BaseActivity activity, Bitmap bitmap) {
        if (PermissionsChecker.checkPermissions(activity, Constants.READ_WRITE)) {
            BitmapUtils.saveBitmap(activity, bitmap);
            return;
        }
        UiHelper.showConfirmDialog(activity, "权限申请", "需要获取“读写手机存储”权限，开启后将可以用于图片和视频的保存功能",
                (dialog, which) -> {
                    switch (which) {
                        case DialogInterface.BUTTON_POSITIVE:
                            activity.addSubscribe(new RxPermissions(activity)
                                    .request(Constants.READ_WRITE)
                                    .subscribe(aBoolean -> {
                                        if (!aBoolean) {
                                            activity.showToast("没有权限，无法保存图片");
                                            return;
                                        }
                                        BitmapUtils.saveBitmap(activity, bitmap);
                                    }));
                            break;
                        case DialogInterface.BUTTON_NEGATIVE:
                            ToastUtils.showLong("没有权限，无法保存图片");
                            break;
                    }
                });
    }
}
